package com.dfbz.controller;

import com.dfbz.entity.Article;
import com.dfbz.entity.PageEntity;
import com.dfbz.service.ArticleService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class articleServlet extends baseServlet {
    private ArticleService articleService = new ArticleService();

    /**
     * 分页+搜索
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String> param = getParam(request);
        Integer currPage = Integer.parseInt(getParam(request).get("currPage"));
        String title = param.get("title");
        PageEntity<Article> pageData = articleService.findByTitle(title, currPage);
        request.setAttribute("pageData", pageData);
        request.setAttribute("title", title);
        request.getRequestDispatcher("/html/article.jsp").forward(request, response);
    }

    /**
     * 发文章
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        // 设置编码
        request.setCharacterEncoding("UTF-8");
        Article article = new Article();
        BeanUtils.populate(article, request.getParameterMap());
        // 发布人信息
        article.setUserId(loginUser.getId());
        article.setPublishRealName(loginUser.getRealName());

        articleService.add(article);

        // 添加成功后重新转发到findPage填充数据
        response.sendRedirect("/article/findPage?currPage=1");
    }

    /**
     * 文章数据回显
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void articleDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        Map<String, String> param = getParam(request);
        //文章id
        Long id = Long.parseLong(param.get("id"));
//                - 文章的浏览量应该+1
//                - Article对象
//
//                - 收藏次数
//                - 我是否有收藏过这篇文章
//                - 我关注的人，并且他们也收藏了这篇文章
        // 浏览量+1
        articleService.updateBrowserCount(id);

        // 文章详情
        Article article = articleService.findById(id);

        // 收藏次数
        Integer favoriteCount = articleService.countFavoriteByArticleId(id);

        // 我是收藏过这篇文章
        Boolean isFavorite = articleService.isFavorite(loginUser.getId(), id);

        // 我关注的人里面也收藏过这篇文章的人
        List<Map<String, Object>> userFavorite = articleService.findUserFavorite(loginUser.getId(), id);
        request.setAttribute("article", article);
        request.setAttribute("isFavorite", isFavorite);
        request.setAttribute("userFavorite", userFavorite);
        request.setAttribute("favoriteCount", favoriteCount);

        request.getRequestDispatcher("/html/article_detail.jsp").forward(request, response);
    }

    /**
     * 收藏
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void favorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        try {
            Map<String, String> param = getParam(request);
            Boolean isFavorite = Boolean.parseBoolean(param.get("isFavorite"));
            Long id = Long.parseLong(param.get("id"));
            if (isFavorite) {
                //取消搜藏
                articleService.unFoucs(loginUser.getId(), id);
            } else {
                articleService.Foucs(loginUser.getId(), id);
            }
            writerObjToString(response, 200);
        } catch (NumberFormatException e) {
            writerObjToString(response, 500);
            e.printStackTrace();
        }
    }

    /**
     *
     */
    public void favoritePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        Map<String, String> param = getParam(request);

        Integer currPage = Integer.parseInt(param.get("currPage"));

        String title = param.get("title");

        PageEntity<Article> pageData = articleService.favoritePage(currPage, title, loginUser.getId());

        request.setAttribute("pageData", pageData);

        // 用于回显搜索条件
        request.setAttribute("title", title);
        request.getRequestDispatcher("/html/article_collect.jsp").forward(request, response);
    }
}
